package ipc.project;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemLongClickListener;

public class MainActivity extends Activity implements OnClickListener {

	public Context context = this;
	private User user = new User();
	public Handler onlinersMonitor;

	Button btnSearch;
	Button btnLogOut;
	Button btnChooseStatus;
	Button btnUpdateList;
	Button btnIncomingRequests;

	CheckBox ChckbxAll;
	
	TextView userName;
	TextView textCount;

	ListView lstFriends;
	Friend selectedItem;
	FriendsAdapter adapter;
	View header;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main_screen);
		user = ((App) getApplication()).getUser();

		userName = (TextView) findViewById(R.id.textLogin);
		userName.setText(user.getLogin());
		btnChooseStatus = (Button) findViewById(R.id.buttonChooseStatus);
		btnChooseStatus.setText(user.getStatus());
		btnChooseStatus.setOnClickListener(this);

		lstFriends = (ListView) findViewById(R.id.listFriends);

		header = (View) getLayoutInflater().inflate(R.layout.listview_header_row, null);
		lstFriends.addHeaderView(header);
		
		ChckbxAll = (CheckBox) findViewById(R.id.checkBox1);
		ChckbxAll.setOnClickListener(this);
		
		adapter = new FriendsAdapter(this, R.layout.listview_item_row, user.getFriends(ChckbxAll.isChecked()));
		lstFriends.setAdapter(adapter);

		textCount = (TextView) findViewById(R.id.txtCount);
		textCount.setText(user.getCountOfOnlineFriends());

		lstFriends.setOnItemLongClickListener(new OnItemLongClickListener() {
			public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {

				selectedItem = (Friend) parent.getItemAtPosition(position);

				AlertDialog.Builder builder = new AlertDialog.Builder(context);
				builder.setMessage("�� ������ ������� " + selectedItem.login + "?");
				builder.setCancelable(false);
				builder.setPositiveButton("��", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						user.deleteFriend(selectedItem.login);
						adapter.remove(selectedItem);
						adapter.notifyDataSetChanged();
					}
				});
				builder.setNegativeButton("���", new DialogInterface.OnClickListener() {
					public void onClick(DialogInterface dialog, int which) {
						dialog.cancel();
					}
				});
				builder.show();
				return true;
			}
		});

		onlinersMonitor = new Handler() {
			public void handleMessage(android.os.Message message) {
				switch (message.what) {
				case 1:
					adapter = new FriendsAdapter(context, R.layout.listview_item_row, user
									.getFriends(ChckbxAll.isChecked()));
					lstFriends.setAdapter(adapter);
					textCount.setText(user.getCountOfOnlineFriends());
					break;
				}
			}
		};

		ConnectionWithServer.setFriendsHandler(onlinersMonitor);

		btnSearch = (Button) findViewById(R.id.buttonAddNewFriend);
		btnSearch.setOnClickListener(this);

		btnIncomingRequests = (Button) findViewById(R.id.buttonIncomingRequests);
		btnIncomingRequests.setOnClickListener(this);
	}

	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 0, 0, "�����");
		return super.onCreateOptionsMenu(menu);

	}

	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.buttonAddNewFriend:
			startActivity(new Intent(context, AddFriendActivity.class));
			break;
		case R.id.buttonChooseStatus:
			startActivity(new Intent(context, ScreenChooseStatus.class));
			break;
		case R.id.buttonIncomingRequests:
			startActivity(new Intent(context, RequestsActivity.class));
			break;
		case R.id.checkBox1:
			onlinersMonitor.sendEmptyMessage(1);
			break;
		}
	}

	public boolean onOptionsItemSelected(MenuItem item) {
		user.signOut();
		startActivity(new Intent(context, StartActivity.class));
		finish();
		return super.onOptionsItemSelected(item);

	}

}
